﻿namespace Smart.Utils.Net
{
    using System;
    using System.Windows.Forms;

    public class Rtf2Html
    {
        private string backgroundColour;
        private bool bold;
        private string font;
        private bool italic;
        private long originalLength;
        private long originalStart;
        private short size;
        private string textColour;
        private bool underline;

        public string ConvertRtfToHtml(RichTextBox Box)
        {
            string str = "";
            string str2 = "";
            string str3 = "";
            if (Box.Text.Length == 0)
            {
                return "<html></html>";
            }
            this.originalStart = 0L;
            this.originalLength = Box.Text.Length;
            Box.Select(0, 1);
            str = "<html>";
            this.textColour = "#" + Box.SelectionColor.R.ToString("X2") + Box.SelectionColor.G.ToString("X2") + Box.SelectionColor.B.ToString("X2");
            this.backgroundColour = "#" + Box.SelectionBackColor.R.ToString("X2") + Box.SelectionBackColor.G.ToString("X2") + Box.SelectionBackColor.B.ToString("X2");
            this.bold = Box.SelectionFont.Bold;
            this.italic = Box.SelectionFont.Italic;
            this.underline = Box.SelectionFont.Underline;
            this.font = Box.SelectionFont.FontFamily.Name;
            this.size = Convert.ToInt16(Box.SelectionFont.Size);
            object obj2 = str;
            str = string.Concat(new object[] { obj2, "<span style=\"font-family: ", this.font, "; font-size: ", this.size, "pt; color: ", this.textColour, "; background-color: ", this.backgroundColour, "\">" });
            if (this.bold)
            {
                str = str + "<b>";
            }
            if (this.italic)
            {
                str = str + "<i>";
            }
            if (this.underline)
            {
                str = str + "<u>";
            }
            if (Box.Text.Substring(0, 1) == Convert.ToChar(10).ToString())
            {
                str = str + "<br>";
            }
            else if (Box.Text.Substring(0, 1) == " ")
            {
                str = str + "&nbsp;";
            }
            else
            {
                str = str + Box.Text.Substring(0, 1);
            }
            for (int i = 2; i <= Box.Text.Length; i++)
            {
                Box.Select(i - 1, 1);
                if (Box.Text.Substring(i - 1, 1) == Convert.ToChar(10).ToString())
                {
                    str = str + "<br>";
                }
                else if (Box.Text.Substring(i - 1, 1) == " ")
                {
                    str = str + "&nbsp;";
                    continue;
                }
                str2 = "#" + Box.SelectionColor.R.ToString("X2") + Box.SelectionColor.G.ToString("X2") + Box.SelectionColor.B.ToString("X2");
                str3 = "#" + Box.SelectionBackColor.R.ToString("X2") + Box.SelectionBackColor.G.ToString("X2") + Box.SelectionBackColor.B.ToString("X2");
                if (((!str2.Equals(this.textColour, StringComparison.OrdinalIgnoreCase) || !str3.Equals(this.backgroundColour, StringComparison.OrdinalIgnoreCase)) || !Box.SelectionFont.FontFamily.Name.Equals(this.font, StringComparison.OrdinalIgnoreCase)) || (Box.SelectionFont.Size != this.size))
                {
                    obj2 = str;
                    str = string.Concat(new object[] { obj2, "</span><span style=\"font-family: ", Box.SelectionFont.FontFamily.Name, "; font-size: ", Box.SelectionFont.Size, "pt; color: ", str2, "; background-color: ", str3, "\">" });
                }
                if (Box.SelectionFont.Bold != this.bold)
                {
                    if (!Box.SelectionFont.Bold)
                    {
                        str = str + "</b>";
                    }
                    else
                    {
                        str = str + "<b>";
                    }
                }
                if (Box.SelectionFont.Italic != this.italic)
                {
                    if (!Box.SelectionFont.Italic)
                    {
                        str = str + "</i>";
                    }
                    else
                    {
                        str = str + "<i>";
                    }
                }
                if (Box.SelectionFont.Underline != this.underline)
                {
                    if (!Box.SelectionFont.Underline)
                    {
                        str = str + "</u>";
                    }
                    else
                    {
                        str = str + "<u>";
                    }
                }
                str = str + Box.Text.Substring(i - 1, 1);
                this.textColour = "#" + Box.SelectionColor.R.ToString("X2") + Box.SelectionColor.G.ToString("X2") + Box.SelectionColor.B.ToString("X2");
                this.backgroundColour = "#" + Box.SelectionBackColor.R.ToString("X2") + Box.SelectionBackColor.G.ToString("X2") + Box.SelectionBackColor.B.ToString("X2");
                this.bold = Box.SelectionFont.Bold;
                this.italic = Box.SelectionFont.Italic;
                this.underline = Box.SelectionFont.Underline;
                this.font = Box.SelectionFont.FontFamily.Name;
                this.size = Convert.ToInt16(Box.SelectionFont.Size);
            }
            if (this.bold)
            {
                str = str ?? "";
            }
            if (this.italic)
            {
                str = str ?? "";
            }
            if (this.underline)
            {
                str = str ?? "";
            }
            return (str + "</span></html>");
        }
    }
}

